package com.example.demo3.controller;


import com.dsbj.common.msg.DataPageMessage;
import com.dsbj.common.msg.ResponseMessage;
import com.example.demo3.entity.CodeDtc;
import com.example.demo3.entity.DictData;
import com.example.demo3.service.CodeDtcService;
import com.example.demo3.service.DictTypeService;
import com.example.demo3.utils.constant.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 故障码 前端控制器
 * </p>
 *
 * @author xsyuan
 * @since 2020-03-27
 */
@Controller
@RequestMapping("codeDtc")
public class CodeDtcController extends BaseController {

    @Resource
    private DictTypeService dictTypeService;

    @Resource
    private CodeDtcService codeDtcService;


    @RequestMapping("list")
    public void list(){}

    @RequestMapping("codeDtcPageInfo")
    @ResponseBody
    public DataPageMessage codeDtcPageInfo(int pageindex, String orderdir, int pagesize, String ordercolumn, int sEcho, String textVague, String codeVague, String nameVague){
        Map<Object,Object> map = new HashMap<>();
            map.put("pageindex",pageindex);
        map.put("pagesize",pagesize);
        map.put("orderdir",orderdir);
        map.put("ordercolumn",ordercolumn);
        if(StringUtils.isNotBlank(textVague)){
            map.put("textVague",textVague);
        }
        if(StringUtils.isNotBlank(codeVague)){
            map.put("codeVague",codeVague);
        }
        if(StringUtils.isNotBlank(nameVague)){
            map.put("nameVague",nameVague);
        }
        int countPage=codeDtcService.countPage(map);
        List<CodeDtc> codeDtcList=codeDtcService.queryPage(map);
        return DataPageMessage.ok(codeDtcList,countPage,countPage,sEcho,"");
    }

    @RequestMapping("add")
    public String add (Model model){
        List<DictData> dictDataList =dictTypeService.getByType(Constants.DICT_TYPE_DTC_TYPE);
        model.addAttribute("dictDataList",dictDataList);
        return "codeDtc/add";
    }

    @RequestMapping("edit")
    public String edit (Model model, String id){
        if(StringUtils.isNotBlank(id)){
            Map<String,Object> map  = new HashMap<>();
                map.put("id",id);
                map.put("delFlag",0);
            List<CodeDtc> codeDtcList = codeDtcService.getByMap(map);
            if(codeDtcList.size() >0 && codeDtcList != null && !codeDtcList.isEmpty()){
                model.addAttribute("codeDtc",codeDtcList.get(0));
            }
        }
        return "codeDtc/edit";
    }

    @RequestMapping("del")
    @ResponseBody
    public ResponseMessage del (CodeDtc codeDtc){
        codeDtc.setDelFlag(1);
        codeDtcService.update(codeDtc);
        return ResponseMessage.ok("删除成功");
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public ResponseMessage saveOrUpdate(CodeDtc codeDtc){
        codeDtc.setCreateUser("xsyuan");
        codeDtc.setCreateTime(new Date());
        String msg= "";
        if(StringUtils.isNotBlank(codeDtc.getId())){
            codeDtc.setUpdateUser("xsyuan");
            codeDtc.setUpdateTime(new Date());
            codeDtcService.update(codeDtc);
            msg = "修改成功";
        }else{
            codeDtc.setCreateUser("xsyuan");
            codeDtc.setCreateTime(new Date());
            codeDtcService.insert(codeDtc);
            msg = "添加成功";
        }
        return ResponseMessage.ok(msg);
    }

}
